/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * 知启蒙Servlet兼容库（zhiqim_servlet）在LGPL3.0协议下开源：https://www.zhiqim.com/gitcan/zhiqim/zhiqim_servlet.htm
 *
 * This file is part of [zhiqim_servlet].
 * 
 * [zhiqim_servlet] is free software: you can redistribute
 * it and/or modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * [zhiqim_servlet] is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with [zhiqim_servlet].
 * If not, see <http://www.gnu.org/licenses/>.
 */
package org.zhiqim.calendar.action;

import java.util.ArrayList;
import java.util.List;

import org.zhiqim.calendar.ZmrCalendarConstants;
import org.zhiqim.calendar.dbo.ZmrCalendar;
import org.zhiqim.calendar.model.CalendarModel;

import org.zhiqim.kernel.Global;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.core.Action;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.kernel.util.DateTimes;

/**
 * 日历信息表
 *
 * @version v1.0.0 @author huangsufang 2017-10-27 新建与整理
 */
public class CalendarAction implements Action, ZmrCalendarConstants
{
    @Override
    public void execute(HttpRequest request) throws Exception
    {
        int year = request.getParameterInt("year", DateTimes.getCurrentYear());
        int month = request.getParameterInt("month", DateTimes.getCurrentMonth());
        
        boolean isPrev = request.getParameterBoolean("prev");
        boolean isNext = request.getParameterBoolean("next");
        
        if (isPrev)
        {
            year = month==1?year-1:year;
            month = month==1?12:month-1;
        }
        else if (isNext)
        {
            year = month==12?year+1:year;
            month = month==12?1:month+1;
        }
        
        //计算上月和下月，并取出上当下三月数据
        int prevYear = month==1?year-1:year;
        int prevMonth = month==1?12:month-1;
        int nextYear = month==12?year+1:year;
        int nextMonth = month==12?1:month+1;

        Selector selector = new Selector();
        selector.addMustThenG("calendarDate", prevYear * 10000 + prevMonth * 100);
        selector.addMustThenL("calendarDate", nextYear * 10000 + (nextMonth+1) * 100);
        List<ZmrCalendar> calendarList = Global.get(ZTable.class).list(ZmrCalendar.class, selector);
        
        List<CalendarModel> list = new ArrayList<>();
        
        //生成日历表
        int firstWeek = DateTimes.getDateWeek7(year, month, 1); //某月第一天的星期几
        int curMonthMaxDay = DateTimes.getMonthDays(year, month);
        int prevMonthMaxDay = DateTimes.getMonthDays(prevYear, prevMonth);
        
        //1到firstWeek表示上月
        for (int i=1;i<=firstWeek;i++)
        {
            int day = prevMonthMaxDay-firstWeek+i;
            
            CalendarModel date = new CalendarModel();
            date.setCalendarDate(prevYear, prevMonth, day);
            date.setCalendarMonth(PREV_MONTH);
            
            setCalendarDay(calendarList, date);
            list.add(date);
        }
        
        //firstWeek+1到firstWeek+1+curMonthMaxDay表示当月
        for (int i=firstWeek+1;i<firstWeek+1+curMonthMaxDay;i++)
        {
            int day = i-firstWeek;
            
            CalendarModel date = new CalendarModel();
            date.setCalendarDate(year, month, day);
            date.setCalendarMonth(CURR_MONTH);
            
            setCalendarDay(calendarList, date);
            list.add(date);
        }
        
        //firstWeek+1+curMonthMaxDay+1到42表示下月
        for (int i=firstWeek+1+curMonthMaxDay;i<=42;i++)
        {
            int day = i-curMonthMaxDay-firstWeek;//日期从1到显示的最后一天
            
            CalendarModel date = new CalendarModel();
            date.setCalendarDate(nextYear, nextMonth, day);
            date.setCalendarMonth(NEXT_MONTH);
            
            setCalendarDay(calendarList, date);
            list.add(date);
        }
        
        request.setAttribute("year", year);
        request.setAttribute("month", month);
        request.setAttribute("list", list);
    }
    
    private void setCalendarDay(List<ZmrCalendar> calendarList, CalendarModel date)
    {
        ZmrCalendar calendar = getCalendar(calendarList, date.getCalendarDate());
        if (calendar != null)
        {//存在配置取配置
            date.setCalendarRest(calendar.isCalendarRest());
        }
        else
        {//否则周六周末认为是休息日
            int week = DateTimes.getDateWeek7(date.getYear(), date.getMonth(), date.getDay());
            date.setCalendarRest(week == 6 || week == 7);
        }
    }
    
    private ZmrCalendar getCalendar(List<ZmrCalendar> calendarList, int date)
    {
        for (ZmrCalendar calendar : calendarList)
        {
            if (calendar.getCalendarDate() == date)
                return calendar;
        }
        
        return null;
    }
}
